layui.define(function(exports) {
    var $ = layui.$,
      admin = layui.admin,
      setter = layui.setter,
      laytpl = layui.laytpl,
      util = layui.util;

    var list = []; 

    function initList(callback) {
      var params = {
        top: 1
      }

      list = [];

      admin.req({
        url: setter.url + '/product/getProductListMall',
        type: 'GET',
        data: params,
        done: function (res) {
          var data = res.data || [];
          list = data;
          renderData(list);

          if (typeof(callback) == 'function') {
            callback();
          }
        }
      });
    }

    // 初始化方法
    function initEvents() {
      $('.LAY_hot_list_btn_delete').off('click').on('click', function () {
        var productCode = $(this).data('code');
        layer.confirm('是否确定删除该产品?', function(index){
          deleteData(productCode, function () {
            layer.close(index);
          });
        }); 
      });
      
      $('.LAY_hot_list_btn_up').off('click').on('click', function () {
        var index = $(this).data('index');
        
        if (index <= 0) {
          return;
        }

        moveUp(index);
      });
      
      $('.LAY_hot_list_btn_down').off('click').on('click', function () {
        var index = $(this).data('index');
        
        if (index >= (list.length - 1)) {
          return;
        }

        moveDown(index);
      });
    }

    // 爆款上移
    function moveUp(oldIndex) {
      var newIndex = oldIndex - 1;
      list = updataSort(list, 'topTime', newIndex, oldIndex);
      renderData(list);
      saveSetting();
    }

    // 爆款下移
    function moveDown(oldIndex) {
      var newIndex = oldIndex + 1;
      list = updataSort(list, 'topTime', newIndex, oldIndex);
      renderData(list);
      saveSetting();
    }

    // 重新排序
    function updataSort(targetArr, property, newIndex, oldIndex) {
      var clone = $.extend(true, [], targetArr);

      layui.each(clone, function (index, item) {
        delete item.active;
      });

      var cloneNew = $.extend(true, {}, clone[newIndex]);
      var cacheCloneNew = $.extend(true, {}, clone[newIndex]);
      var cloneOld = $.extend(true, {}, clone[oldIndex]);

      cloneNew[property] = cloneOld[property];
      cloneOld[property] = cacheCloneNew[property];
      cloneOld.active = true;

      clone.splice(newIndex, 1, cloneOld);
      clone.splice(oldIndex, 1, cloneNew);

      return clone;
    }

    // 渲染爆款数据
    function renderData(data, callback) { 
      var getTpl = $('#LAY_hot_list_tpl').html();
      
      if (!getTpl) {
        return;
      }

      laytpl(getTpl).render(data, function(html){
        $('#LAY_hot_list_view').html(html);

        if (data && data.length) {
          initEvents();
        }

        if (typeof(callback) === 'function') {
          callback();
        }
      });
    }

    // 查重
    function has(array, target) {
      var buffer = [];

      layui.each(array, function (index, item) {
        buffer.push(Number(item.productCode));
      });

      if (buffer.indexOf(Number(target)) > -1) {
        return true;
      }

      return false;
    }

    // 添加数据
    function addData(array) {
      var buffer = $.extend(true, {}, array);
      var repeatArray = [];

      layui.each(buffer, function (index, item) {
        item.topTime = util.toDateString(Number(new Date().getTime()) + Number(index), 'yyyy-MM-dd HH:mm:ss');
        
        if (!has(list, item.productCode)) {
          list.splice(0, 0, item);
        } else {
          repeatArray.splice(0, 0, item.productCode);
        }
      });

      if (repeatArray.length) {
        layer.alert('产品 ' + repeatArray.toString() + ' 已存在，无法再添加到爆款列表', {icon: 3});
        return;
      }
      
      renderData(list);
      saveSetting();
    }

    // 删除数据
    function deleteData(productCode, callback) {
      var buffer = [];

      layui.each(list, function (index, item) { 
        if (item.productCode != productCode) {
          buffer.push(item);
        }
      });

      list = buffer;
      renderData(list);
      saveSetting(callback);
    }
    
    // 储存爆款设置
    function saveSetting(callback) {
      var _list = $.extend(true, [], list);

      layui.each(_list, function (index, item) {
        delete item.active;
      });

      admin.req({
        url: setter.url + '/product/topSort',
        type: 'POST',
        data: { 
          data: JSON.stringify(_list)
        },
        done: function () {
          if (typeof(callback) == 'function') {
            callback();
          }
        }
      });
    }

    exports('hot', {
      add: addData,
      init: initList,
    });
});